import apiService from "@service/index";
import { saveUser } from "@slice/user";
// import apiService from "@service/index";
import store from "@store/index";

// @ts-ignore
export const userApiService = apiService

  // .enhanceEndpoints({ addTagTypes: ["profile"] })   // 
  // // 获取用户资料
  // .injectEndpoints({
  //   endpoints: (builder) => ({
  //     requestProfile: builder.query<UserProfileResponse, undefined>({
  //       query: () => ({ url: "/user/profile" }),
  //       providesTags: ["profile"], // 

  .enhanceEndpoints({ addTagTypes: ["article"] })
  // 获取用户资料
  //@ts-nocheck
  .injectEndpoints({
   
    endpoints: (builder) => ({

     
      requestProfile: builder.query<UserProfileResponse, undefined>({
        query: () => ({ url: "/user/profile" }),
        providesTags: ["article"], // 
        //@ts-ignore
        async onQueryStarted(arg, api) {
          const response = await api.queryFulfilled;
          api.dispatch(saveUser(response.data.data));
        },
      }),
      followUser: builder.mutation({
        query: (id: string) => ({
          url: "/user/followings",
          method: "POST",
          body: { target: id },
        }),
        invalidatesTags: ["article"],
      }),
      // 取消关注用户
      unFollowUser: builder.mutation({
        query: (id: string) => ({ url: `/user/followings/${id}`, method: "DELETE" }),
        invalidatesTags: ["article"],
      }),

     
      requestUser: builder.query<UserResponse, undefined>({
        query: () => ({
          url: "/user",
          headers: {
            authorization: `Bearer ${store.getState().credentialsReducer.token}`,
          },
        }),
      }),

     
      uploadAvatar: builder.mutation<UploadAvatarResponse, FormData>({
        //@ts-ignore
        query: (formData) => ({
          url: "/user/photo",
          method: "PATCH",
          body: formData,
        }),
        invalidatesTags: ["profile" as any],
      }),

     
      updateProfile: builder.mutation<GeekResponse<null>, Partial<UserProfile>>({
        //@ts-ignore
        query: (profile) => ({ url: "/user/profile", method: "PATCH", body: profile }),
        invalidatesTags: ["profile" as any],
      }),


    
     
     
     

    }),
  })
export const { useLazyRequestProfileQuery, useUploadAvatarMutation,useFollowUserMutation, useUnFollowUserMutation,useRequestUserQuery,useRequestProfileQuery,useUpdateProfileMutation} = userApiService






